home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Chapter 13 The Compute Shader / SobelFilter / Shaders / Default.hlsl < prev    next >
Encoding:
Text File  |  2016-03-02  |  5.1 KB  |  177 lines

  1. //***************************************************************************************
  2. // Default.hlsl by Frank Luna (C) 2015 All Rights Reserved.
  3. //
  4. // Default shader, currently supports lighting.
  5. //***************************************************************************************
  6.  
  7. // Defaults for number of lights.
  8. #ifndef NUM_DIR_LIGHTS
  9.     #define NUM_DIR_LIGHTS 3
  10. #endif
  11.  
  12. #ifndef NUM_POINT_LIGHTS
  13.     #define NUM_POINT_LIGHTS 0
  14. #endif
  15.  
  16. #ifndef NUM_SPOT_LIGHTS
  17.     #define NUM_SPOT_LIGHTS 0
  18. #endif
  19.  
  20. // Include structures and functions for lighting.
  21. #include "LightingUtil.hlsl"
  22.  
  23. Texture2D    gDiffuseMap : register(t0);
  24. Texture2D    gDisplacementMap : register(t1);
  25.  
  26. SamplerState gsamPointWrap        : register(s0);
  27. SamplerState gsamPointClamp       : register(s1);
  28. SamplerState gsamLinearWrap       : register(s2);
  29. SamplerState gsamLinearClamp      : register(s3);
  30. SamplerState gsamAnisotropicWrap  : register(s4);
  31. SamplerState gsamAnisotropicClamp : register(s5);
  32.  
  33. // Constant data that varies per frame.
  34. cbuffer cbPerObject : register(b0)
  35. {
  36.     float4x4 gWorld;
  37.     float4x4 gTexTransform;
  38.     float2 gDisplacementMapTexelSize;
  39.     float gGridSpatialStep;
  40.     float cbPerObjectPad1;
  41. };
  42.  
  43. // Constant data that varies per material.
  44. cbuffer cbPass : register(b1)
  45. {
  46.     float4x4 gView;
  47.     float4x4 gInvView;
  48.     float4x4 gProj;
  49.     float4x4 gInvProj;
  50.     float4x4 gViewProj;
  51.     float4x4 gInvViewProj;
  52.     float3 gEyePosW;
  53.     float cbPerPassPad1;
  54.     float2 gRenderTargetSize;
  55.     float2 gInvRenderTargetSize;
  56.     float gNearZ;
  57.     float gFarZ;
  58.     float gTotalTime;
  59.     float gDeltaTime;
  60.     float4 gAmbientLight;
  61.  
  62.     float4 gFogColor;
  63.     float gFogStart;
  64.     float gFogRange;
  65.     float2 cbPerPassPad2;
  66.  
  67.     // Indices [0, NUM_DIR_LIGHTS) are directional lights;
  68.     // indices [NUM_DIR_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHTS) are point lights;
  69.     // indices [NUM_DIR_LIGHTS+NUM_POINT_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHT+NUM_SPOT_LIGHTS)
  70.     // are spot lights for a maximum of MaxLights per object.
  71.     Light gLights[MaxLights];
  72. };
  73.  
  74. cbuffer cbMaterial : register(b2)
  75. {
  76.     float4   gDiffuseAlbedo;
  77.     float3   gFresnelR0;
  78.     float    gRoughness;
  79.     float4x4 gMatTransform;
  80. };
  81.  
  82. struct VertexIn
  83. {
  84.     float3 PosL    : POSITION;
  85.     float3 NormalL : NORMAL;
  86.     float2 TexC    : TEXCOORD;
  87. };
  88.  
  89. struct VertexOut
  90. {
  91.     float4 PosH    : SV_POSITION;
  92.     float3 PosW    : POSITION;
  93.     float3 NormalW : NORMAL;
  94.     float2 TexC    : TEXCOORD;
  95. };
  96.  
  97. VertexOut VS(VertexIn vin)
  98. {
  99.     VertexOut vout = (VertexOut)0.0f;
  100.     
  101.     
  102. #ifdef DISPLACEMENT_MAP
  103.     // Sample the displacement map using non-transformed [0,1]^2 tex-coords.
  104.     vin.PosL.y += gDisplacementMap.SampleLevel(gsamLinearWrap, vin.TexC, 1.0f).r;
  105.     
  106.     // Estimate normal using finite difference.
  107.     float du = gDisplacementMapTexelSize.x;
  108.     float dv = gDisplacementMapTexelSize.y;
  109.     float l = gDisplacementMap.SampleLevel( gsamPointClamp, vin.TexC-float2(du, 0.0f), 0.0f ).r;
  110.     float r = gDisplacementMap.SampleLevel( gsamPointClamp, vin.TexC+float2(du, 0.0f), 0.0f ).r;
  111.     float t = gDisplacementMap.SampleLevel( gsamPointClamp, vin.TexC-float2(0.0f, dv), 0.0f ).r;
  112.     float b = gDisplacementMap.SampleLevel( gsamPointClamp, vin.TexC+float2(0.0f, dv), 0.0f ).r;
  113.     vin.NormalL = normalize( float3(-r+l, 2.0f*gGridSpatialStep, b-t) );
  114.     
  115. #endif
  116.     
  117.     
  118.     // Transform to world space.
  119.     float4 posW = mul(float4(vin.PosL, 1.0f), gWorld);
  120.     vout.PosW = posW.xyz;
  121.  
  122.     // Assumes nonuniform scaling; otherwise, need to use inverse-transpose of world matrix.
  123.     vout.NormalW = mul(vin.NormalL, (float3x3)gWorld);
  124.  
  125.     // Transform to homogeneous clip space.
  126.     vout.PosH = mul(posW, gViewProj);
  127.     
  128.     // Output vertex attributes for interpolation across triangle.
  129.     float4 texC = mul(float4(vin.TexC, 0.0f, 1.0f), gTexTransform);
  130.     vout.TexC = mul(texC, gMatTransform).xy;
  131.  
  132.     return vout;
  133. }
  134.  
  135. float4 PS(VertexOut pin) : SV_Target
  136. {
  137.     float4 diffuseAlbedo = gDiffuseMap.Sample(gsamAnisotropicWrap, pin.TexC) * gDiffuseAlbedo;
  138.     
  139. #ifdef ALPHA_TEST
  140.     // Discard pixel if texture alpha < 0.1.  We do this test as soon 
  141.     // as possible in the shader so that we can potentially exit the
  142.     // shader early, thereby skipping the rest of the shader code.
  143.     clip(diffuseAlbedo.a - 0.1f);
  144. #endif
  145.  
  146.     // Interpolating normal can unnormalize it, so renormalize it.
  147.     pin.NormalW = normalize(pin.NormalW);
  148.  
  149.     // Vector from point being lit to eye. 
  150.     float3 toEyeW = gEyePosW - pin.PosW;
  151.     float distToEye = length(toEyeW);
  152.     toEyeW /= distToEye; // normalize
  153.  
  154.     // Light terms.
  155.     float4 ambient = gAmbientLight*diffuseAlbedo;
  156.  
  157.     const float shininess = 1.0f - gRoughness;
  158.     Material mat = { diffuseAlbedo, gFresnelR0, shininess };
  159.     float3 shadowFactor = 1.0f;
  160.     float4 directLight = ComputeLighting(gLights, mat, pin.PosW,
  161.         pin.NormalW, toEyeW, shadowFactor);
  162.  
  163.     float4 litColor = ambient + directLight;
  164.  
  165. #ifdef FOG
  166.     float fogAmount = saturate((distToEye - gFogStart) / gFogRange);
  167.     litColor = lerp(litColor, gFogColor, fogAmount);
  168. #endif
  169.  
  170.     // Common convention to take alpha from diffuse albedo.
  171.     litColor.a = diffuseAlbedo.a;
  172.  
  173.     return litColor;
  174. }
  175.  
  176.  
  177.